/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package caa.struts.faculty.action;

import caa.struts.faculty.form.ManageFacultyForm;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;
import th.com.entity.BranchEntity;
import th.com.entity.DepartmentEntity;
import th.com.entity.DisplayDTO;
import th.com.entity.FacultyEntity;
import th.com.entity.ProgramEntity;
import th.com.entity.SubjectEntity;
import th.com.entity.UserEntity;
import th.com.ku.CAAService;

/**
 *
 * @author omega
 */
public class FacultyManageAction extends DispatchAction {

    public ActionForward doNewFaculty(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
        ManageFacultyForm addFacultyForm = (ManageFacultyForm) form;
        ArrayList<FacultyEntity> facList = CAAService.selectFacultyAll();
        addFacultyForm.setFacultyList(facList);

        addFacultyForm.setDepartmentList(new ArrayList<DepartmentEntity>());
        addFacultyForm.setBranchList(new ArrayList<BranchEntity>());
        addFacultyForm.setProgramList(new ArrayList<ProgramEntity>());

        addFacultyForm.setSubjectEntity(new SubjectEntity());
        addFacultyForm.getProgramEntity().setProId("");
        addFacultyForm.getProgramEntity().setProName("");
        addFacultyForm.getBranchEntity().setBraId("");
        addFacultyForm.getBranchEntity().setBraName("");
        addFacultyForm.getDepartmentEntity().setDepId("");
        addFacultyForm.getDepartmentEntity().setDepName("");
        addFacultyForm.getFacultyEntity().setFacId("");
        addFacultyForm.getFacultyEntity().setFacName("");
        
        ArrayList<DisplayDTO> dtoList = new ArrayList<DisplayDTO>();
        ArrayList<FacultyEntity> list = addFacultyForm.getFacultyList();
        for (FacultyEntity entity : list) {
            DisplayDTO dto = new DisplayDTO();
            dto.setId(entity.getFacId());
            dto.setName(entity.getFacName());
            dtoList.add(dto);
        }
        addFacultyForm.setDtoList(dtoList);
        addFacultyForm.setKnowClass(FacultyEntity.class);
        System.out.println("dd = " + addFacultyForm.getFacultyEntity().getFacId());

        return mapping.findForward("addnewfaculty.success");

    }

    public ActionForward doManageFaculty(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
        ManageFacultyForm addFacultyForm = (ManageFacultyForm) form;
        //สร้าง listItem
//        ArrayList<FacultyEntity> facList = CAAService.selectFacultyAll();
//        addFacultyForm.setFacultyList(facList);
//        System.out.println("dd = " + addFacultyForm.getFacultyEntity().getFacId());
        if (addFacultyForm.getFacultyEntity() != null) {
            System.out.println("addFacultyForm.getFacultyEntity().getFacId() = " + addFacultyForm.getFacultyEntity().getFacId());
            List<DepartmentEntity> depList = CAAService.selectDepartmentAll(addFacultyForm.getFacultyEntity().getFacId());
            addFacultyForm.setDepartmentList(new ArrayList<DepartmentEntity>(depList));
        }
        if (addFacultyForm.getDepartmentEntity() != null) {
            System.out.println("addFacultyForm.getDepartmentEntity().getDepId() = " + addFacultyForm.getDepartmentEntity().getDepId());
            List<BranchEntity> braList = CAAService.selectBranchAll(addFacultyForm.getDepartmentEntity().getDepId());
            addFacultyForm.setBranchList(new ArrayList<BranchEntity>(braList));
        }
        if (addFacultyForm.getBranchEntity() != null) {
            System.out.println("addFacultyForm.getBranchEntity().getBraId() = " + addFacultyForm.getBranchEntity().getBraId());
            List<ProgramEntity> proList = CAAService.selectProgramAll(addFacultyForm.getBranchEntity().getBraId());
            addFacultyForm.setProgramList(new ArrayList<ProgramEntity>(proList));
        }
        if (addFacultyForm.getProgramEntity() != null) {
            System.out.println("addFacultyForm.getProgramEntity().getProId() = " + addFacultyForm.getProgramEntity().getProId());
            List<SubjectEntity> subList = CAAService.selectSubjectAll(addFacultyForm.getProgramEntity().getProId());
            addFacultyForm.setSubjectList(new ArrayList<SubjectEntity>(subList));
        }

        ArrayList<DisplayDTO> dtoList = new ArrayList<DisplayDTO>();
        if (!addFacultyForm.getProgramEntity().getProId().equalsIgnoreCase("")) {
            List<SubjectEntity> list = addFacultyForm.getSubjectList();
            for (SubjectEntity entity : list) {
                DisplayDTO dto = new DisplayDTO();
                dto.setId(entity.getSubId());
                dto.setName(entity.getSubNameEng());
                dtoList.add(dto);
            }
            addFacultyForm.setKnowClass(SubjectEntity.class);
        } else if (!addFacultyForm.getBranchEntity().getBraId().equalsIgnoreCase("")) {
            List<ProgramEntity> list = addFacultyForm.getProgramList();
            for (ProgramEntity entity : list) {
                DisplayDTO dto = new DisplayDTO();
                dto.setId(entity.getProId());
                dto.setName(entity.getProName());
                dtoList.add(dto);
            }
            addFacultyForm.setKnowClass(ProgramEntity.class);
        } else if (!addFacultyForm.getDepartmentEntity().getDepId().equalsIgnoreCase("")) {
            List<BranchEntity> list = addFacultyForm.getBranchList();
            for (BranchEntity entity : list) {
                DisplayDTO dto = new DisplayDTO();
                dto.setId(entity.getBraId());
                dto.setName(entity.getBraName());
                dtoList.add(dto);
            }
            addFacultyForm.setKnowClass(BranchEntity.class);
        } else if (!addFacultyForm.getFacultyEntity().getFacId().equalsIgnoreCase("")) {
            List<DepartmentEntity> list = addFacultyForm.getDepartmentList();
            for (DepartmentEntity entity : list) {
                DisplayDTO dto = new DisplayDTO();
                dto.setId(entity.getDepId());
                dto.setName(entity.getDepName());
                dtoList.add(dto);
            }
            addFacultyForm.setKnowClass(DepartmentEntity.class);
        } else {
            ArrayList<FacultyEntity> list = addFacultyForm.getFacultyList();
            for (FacultyEntity entity : list) {
                DisplayDTO dto = new DisplayDTO();
                dto.setId(entity.getFacId());
                dto.setName(entity.getFacName());
                dtoList.add(dto);
            }
            addFacultyForm.setKnowClass(FacultyEntity.class);
        }
        addFacultyForm.setDtoList(dtoList);

        return mapping.findForward("managefaculty.success");

    }
    
    public ActionForward doPrePare(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
        ManageFacultyForm addFacultyForm = (ManageFacultyForm) form;
        ActionForward forward = new ActionForward();
        boolean edit = false;
        
        if(addFacultyForm.getMode().equalsIgnoreCase("edit")) {
            edit = true;
        }
        System.out.println(addFacultyForm.getKnowClass().toString());
        System.out.println("class "+DepartmentEntity.class.getName());
        
        
        
        if (addFacultyForm.getKnowClass().getName().equalsIgnoreCase(SubjectEntity.class.getName())) {
            if(edit) {
                SubjectEntity entity = CAAService.findSubject(addFacultyForm.getId());
                addFacultyForm.setSubjectEntity(entity);
            }
            forward = mapping.findForward("addsubject.success");
        } else if (addFacultyForm.getKnowClass().getName().equalsIgnoreCase(ProgramEntity.class.getName())) {
            if(edit) {
                ProgramEntity entity = CAAService.findProgram(addFacultyForm.getId());
                addFacultyForm.setProgramEntity(entity);
            }
            forward = mapping.findForward("addprogram.success");
        } else if (addFacultyForm.getKnowClass().getName().equalsIgnoreCase(BranchEntity.class.getName())) {
            if(edit) {
                BranchEntity entity = CAAService.findBranch(addFacultyForm.getId());
                addFacultyForm.setBranchEntity(entity);
            }
            forward = mapping.findForward("addbranch.success");
        } else if (addFacultyForm.getKnowClass().getName().equalsIgnoreCase(DepartmentEntity.class.getName())) {
            if(edit) {
                DepartmentEntity entity = CAAService.findDepartment(addFacultyForm.getId());
                addFacultyForm.setDepartmentEntity(entity);
            }
            forward = mapping.findForward("adddepartment.success");
        } else {
            if(edit) {
                FacultyEntity entity = CAAService.findFaculty(addFacultyForm.getId());
                addFacultyForm.setFacultyEntity(entity);
            }
            
            forward = mapping.findForward("addfaculty.success");
        }

//        String rs = CAAService.createFaculty(addFacultyForm.getFacultyEntity());
//        addFacultyForm.setFacultyEntity(new FacultyEntity());

        return forward;

    }
    
    public ActionForward doEdit(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
        ManageFacultyForm addFacultyForm = (ManageFacultyForm) form;
        String userId = ((UserEntity)request.getSession().getAttribute("isUsername")).getUseId();
        
        System.out.println(addFacultyForm.getKnowClass().toString());
        System.out.println("class "+DepartmentEntity.class.getName());
        
        if (addFacultyForm.getKnowClass().getName().equalsIgnoreCase(SubjectEntity.class.getName())) {
            CAAService.updateFaculty(addFacultyForm.getSubjectEntity(), userId);
        } else if (addFacultyForm.getKnowClass().getName().equalsIgnoreCase(ProgramEntity.class.getName())) {
            CAAService.updateFaculty(addFacultyForm.getProgramEntity(), userId);
        } else if (addFacultyForm.getKnowClass().getName().equalsIgnoreCase(BranchEntity.class.getName())) {
            CAAService.updateFaculty(addFacultyForm.getBranchEntity(), userId);
        } else if (addFacultyForm.getKnowClass().getName().equalsIgnoreCase(DepartmentEntity.class.getName())) {
            CAAService.updateFaculty(addFacultyForm.getDepartmentEntity(), userId);
        } else {
            CAAService.updateFaculty(addFacultyForm.getFacultyEntity(), userId);
        }
        
        addFacultyForm.setMode("");

        return mapping.findForward("managefaculty.do");

    }
    
    public ActionForward doDelete(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
        ManageFacultyForm addFacultyForm = (ManageFacultyForm) form;
        
        System.out.println(addFacultyForm.getKnowClass().toString());
        System.out.println("class "+DepartmentEntity.class.getName());
        
        if (addFacultyForm.getKnowClass().getName().equalsIgnoreCase(SubjectEntity.class.getName())) {
            CAAService.deleteSubject(addFacultyForm.getId());
        } else if (addFacultyForm.getKnowClass().getName().equalsIgnoreCase(ProgramEntity.class.getName())) {
            CAAService.deleteProgram(addFacultyForm.getId());
        } else if (addFacultyForm.getKnowClass().getName().equalsIgnoreCase(BranchEntity.class.getName())) {
            CAAService.deleteBranch(addFacultyForm.getId());
        } else if (addFacultyForm.getKnowClass().getName().equalsIgnoreCase(DepartmentEntity.class.getName())) {
            CAAService.deleteDepertment(addFacultyForm.getId());
        } else {
            CAAService.deleteFaculty(addFacultyForm.getId());
        }
        
        addFacultyForm.setMode("");

        return mapping.findForward("managefaculty.do");

    }

    public ActionForward doAddFaculty(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
        ManageFacultyForm addFacultyForm = (ManageFacultyForm) form;    
        
        String rs = CAAService.createFaculty(addFacultyForm.getFacultyEntity());
        addFacultyForm.setFacultyEntity(new FacultyEntity());

        return mapping.findForward("managefaculty.do");

    }

//    public ActionForward doManageDepertment(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
//        ManageFacultyForm addFacultyForm = (ManageFacultyForm) form;
//        List<DepartmentEntity> rs = CAAService.selectDepartment(addFacultyForm.getKwKeyWord());
//        addFacultyForm.setDepartmentList(new ArrayList<DepartmentEntity>(rs));
//
//        return mapping.findForward("managedepartment.success");
//
//    }

    public ActionForward doAddDepertment(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
        ManageFacultyForm addFacultyForm = (ManageFacultyForm) form;

        addFacultyForm.getDepartmentEntity().setFacId(addFacultyForm.getFacultyEntity().getFacId());
        String rs = CAAService.createDepartment(addFacultyForm.getDepartmentEntity());

        return mapping.findForward("managefaculty.do");

    }

    public ActionForward doAddBranch(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
        ManageFacultyForm addFacultyForm = (ManageFacultyForm) form;
        
        addFacultyForm.getBranchEntity().setDepId(addFacultyForm.getDepartmentEntity().getDepId());
        String rs = CAAService.createBranch(addFacultyForm.getBranchEntity());

        return mapping.findForward("managefaculty.do");

    }

    public ActionForward doAddProgram(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
        ManageFacultyForm addFacultyForm = (ManageFacultyForm) form;
        
        addFacultyForm.getProgramEntity().setBraId(addFacultyForm.getBranchEntity().getBraId());
        String rs = CAAService.createProgram(addFacultyForm.getProgramEntity());

        return mapping.findForward("managefaculty.do");

    }

//    public ActionForward doManageSubject(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
//        ManageFacultyForm addFacultyForm = (ManageFacultyForm) form;
//        List<SubjectEntity> rs = CAAService.selectSubject(addFacultyForm.getKwKeyWord());
//        addFacultyForm.setSubjectList(new ArrayList<SubjectEntity>(rs));
//
//        return mapping.findForward("managesubject.success");
//
//    }

    public ActionForward doAddSubject(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
        ManageFacultyForm addFacultyForm = (ManageFacultyForm) form;
        
        addFacultyForm.getSubjectEntity().setProId(addFacultyForm.getProgramEntity().getProId());
        String rs = CAAService.createSubject(addFacultyForm.getSubjectEntity());

        return mapping.findForward("managefaculty.do");

    }

    public ActionForward doEditSubject(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
        ManageFacultyForm addFacultyForm = (ManageFacultyForm) form;

        String rs = CAAService.updateSubject(addFacultyForm.getSubjectEntity());

        return mapping.findForward("editsubject.success");

    }

    public ActionForward doPreviewSubject(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
        ManageFacultyForm addFacultyForm = (ManageFacultyForm) form;
//        List<SubjectEntity> rs = CAAService.selectSubjectAll(addFacultyForm.getSubjectId());
//        addFacultyForm.setSubjectEntity(rs.get(0));
        return mapping.findForward("previewsubject.success");
    }
}
